﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Text;
using System.Windows.Forms;

namespace GPSMap
{
    public partial class frmSetting : Form
    {
        public bool nonNumberEntered = false;
        public frmSetting()
        {
            InitializeComponent();
        }

        // Handle the KeyDown event to determine the type of character entered into the control. 
        private void TextBoxNumber_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            // Initialize the flag to false.
            nonNumberEntered = false;

            // Determine whether the keystroke is a number from the top of the keyboard. 
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
            {
                // Determine whether the keystroke is a number from the keypad. 
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                {
                    // A non-numerical keystroke was pressed. 
                    // Set the flag to true and evaluate in KeyPress event.
                    nonNumberEntered = true;
                }
            }
            //If shift key was pressed, it's not a number. 
            if (Control.ModifierKeys == Keys.Shift)
            {
                nonNumberEntered = true;
            }
        }


        // This event occurs after the KeyDown event and can be used to prevent 
        // characters from entering the control. 
        private void TextBoxNumber_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            // Check for the flag being set in the KeyDown event. 
            if (nonNumberEntered == true)
            {
                // Stop the character from being entered into the control since it is non-numerical.
                e.Handled = true;
            }
        }

       
        private void btnDefault_Click(object sender, EventArgs e)
        {
            txtTinh.Text = "500";
            txtHuyen.Text = "200";
            txtXa.Text = "100";
            txtMinDistance.Text = "3";
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Tinh = txtTinh.Text;
            Properties.Settings.Default.Huyen = txtHuyen.Text;
            Properties.Settings.Default.Xa = txtXa.Text;
            Properties.Settings.Default.MinDistance = txtMinDistance.Text;
            Properties.Settings.Default.Save();
            MessageBox.Show("Thông tin thiết lập đã được lưu lại.");
        }

        private void frmSetting_Load(object sender, EventArgs e)
        {
            foreach (Control c in this.tabPage3.Controls)
            {
                c.KeyPress += new KeyPressEventHandler(TextBoxNumber_KeyPress);
                c.KeyDown += new KeyEventHandler(TextBoxNumber_KeyDown);
            }

            txtTinh.Text = Properties.Settings.Default.Tinh;
            txtHuyen.Text = Properties.Settings.Default.Huyen;
            txtXa.Text = Properties.Settings.Default.Xa;
            txtMinDistance.Text = Properties.Settings.Default.MinDistance;

        }
    }
}
